﻿/*
   Copyright 2011 Softisis

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Linq.Expressions;
using FOM.Interfaces;
using FOM.Utils;

namespace FOM.Impl
{
    /// <summary>
    /// This proxy class is used to provide a fluent continuity when providing an overriding class
    /// for the source or target.
    /// </summary>
    /// <remarks>Copyright Softisis 2011 (France)</remarks>
    /// <typeparam name="TSrc">Type of the source object</typeparam>
    /// <typeparam name="TDst">Type of the destination "new" object</typeparam>
    public class ProxyOverTypeNodeImpl<TSrc, TDst> : IMappingNode<TSrc, TDst>, IProxyNode
    {
        private readonly AbstractMappingNode _baseNode;

        /// <summary>
        /// Get the base node on which the proxy node applies to.
        /// </summary>
        public AbstractMappingNode BaseNode
        {
            get { return _baseNode; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ProxyOverTypeNodeImpl(AbstractMappingNode baseNode)
        {
            _baseNode = baseNode;
        }

        public IMappingNode<TPropertyS, TPropertyD> Map<TPropertyS, TPropertyD>(Expression<Func<TSrc, TPropertyS>> expsrc, Expression<Func<TDst, TPropertyD>> expdst)
        {
            var newmap = PathHelper.BuildMappingFromExpression(expsrc, expdst);
            _baseNode.AddMapping(newmap);
            return newmap;
        }

        public IMappingNode<TProperty, TProperty> Map<TProperty>(Expression<Func<TSrc, TProperty>> expr)
        {
            return Map<TProperty, TProperty>(expr, null);
        }

        public IMappingNode<TSrc, TDst> Exclude<TPropertyS, TPropertyD>(Expression<Func<TSrc, TPropertyS>> expsrc, Expression<Func<TDst, TPropertyD>> expdst)
        {
            var newmap = PathHelper.BuildMappingFromExpression(expsrc, expdst);
            _baseNode.AddExclusionMapping(newmap);
            return this;
        }

        public IMappingNode<TSrc, TDst> Exclude<TProperty>(Expression<Func<TSrc, TProperty>> expr)
        {
            return Exclude<TProperty, TProperty>(expr, null);
        }

        public IMappingNode<TSrc, TDst> NewInstance()
        {
            return NewInstance(false);
        }

        public IMappingNode<TSrc, TDst> NewInstance(bool reuseCached)
        {
            _baseNode.CreateNewInstance = true;
            _baseNode.ReuseCachedInstance = reuseCached;
            return this;
        }

        public IMappingNode<TPropertyS, TPropertyD> GoTo<TPropertyS, TPropertyD>(Expression<Func<TSrc, TPropertyS>> expsrc, Expression<Func<TDst, TPropertyD>> expdst)
        {
            var newmap = PathHelper.BuildGotoLinkFromExpression(expsrc, expdst);
            _baseNode.AddMapping(newmap);
            return newmap;
        }

        /// <summary>
        /// Cascading convertion
        /// </summary>
        /// <typeparam name="TReplacement"></typeparam>
        /// <returns></returns>
        public IMappingNode<TSrc, TReplacement> OverrideTargetType<TReplacement>()
        {
            // we create a proxy for fluent continuity
            ProxyOverTypeNodeImpl<TSrc, TReplacement> repl = new ProxyOverTypeNodeImpl<TSrc, TReplacement>(_baseNode);
            _baseNode.TargetOverrideType = typeof(TReplacement);
            return repl;
        }

        public IMappingNode<TReplacement, TDst> OverrideSourceType<TReplacement>()
        {
            // we create a proxy for fluent continuity
            ProxyOverTypeNodeImpl<TReplacement, TDst> repl = new ProxyOverTypeNodeImpl<TReplacement, TDst>(_baseNode);
            _baseNode.SourceOverrideType = typeof(TReplacement);
            return repl;
        }

        public IMappingNode<TSrc, TDst> UseConverter(IConverter converter)
        {
            _baseNode.Converter = converter;
            return this;
        }

        public IMappingNode<TSrc, TDst> UseCondition(ICondition cond)
        {
            _baseNode.Condition = cond;
            return this;
        }

        public IMappingNode<TDst, TSrc> Reverse()
        {
            throw new NotImplementedException("This is not yet implemented");
        }

        public bool Remove<TPropertyS, TPropertyD>(Expression<Func<TSrc, TPropertyS>> expsrc, Expression<Func<TDst, TPropertyD>> expdst)
        {
            throw new NotImplementedException();
        }
    }
}
